package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.DataVerification;
import com.ruoyi.common.core.domain.entity.SysArea;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpClients;
import com.ruoyi.system.domain.vo.SysNewsVo;
import com.ruoyi.system.service.ISysAreaService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysNews;
import com.ruoyi.system.service.ISysNewsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 新闻Controller
 *
 * @author ruoyi
 * @date 2022-03-23
 */
@RestController
@RequestMapping("/system/news")
public class SysNewsController extends BaseController
{
    @Autowired
    private ISysNewsService sysNewsService;

    @Autowired
    private ISysAreaService areaService;

    /**
     * 查询新闻列表
     */
    @PreAuthorize("@ss.hasPermi('system:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysNews sysNews)
    {
        startPage();
        List<SysNewsVo> list = sysNewsService.selectSysNewsList(sysNews);
        return getDataTable(list);
    }

    /**
     * 导出新闻列表
     */
    @PreAuthorize("@ss.hasPermi('system:news:export')")
    @Log(title = "新闻", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysNews sysNews)
    {
        List<SysNewsVo> list = sysNewsService.selectSysNewsList(sysNews);
        ExcelUtil<SysNewsVo> util = new ExcelUtil<SysNewsVo>(SysNewsVo.class);
        util.exportExcel(response, list, "新闻数据");
    }

    /**
     * 获取新闻详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:news:query')")
    @GetMapping(value = "/{newsId}")
    public AjaxResult getInfo(@PathVariable("newsId") Long newsId)
    {
        return AjaxResult.success(sysNewsService.selectSysNewsByNewsId(newsId));
    }

    /**
     * 新增新闻
     */
    @DataVerification(name = "新增新闻")
    @PreAuthorize("@ss.hasPermi('system:news:add')")
    @Log(title = "新闻", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysNews sysNews)
    {
        return toAjax(sysNewsService.insertSysNews(sysNews));
    }

    /**
     * 修改新闻
     */
    @DataVerification(name = "修改新闻")
    @PreAuthorize("@ss.hasPermi('system:news:edit')")
    @Log(title = "新闻", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysNews sysNews)
    {
        return toAjax(sysNewsService.updateSysNews(sysNews));
    }

    /**
     * 推荐修改
     */
    @PreAuthorize("@ss.hasPermi('system:news:edit')")
    @Log(title = "新闻", businessType = BusinessType.UPDATE)
    @PutMapping("/changeRecommend")
    public AjaxResult changeRecommend(@RequestBody SysNews sysNews)
    {
        return toAjax(sysNewsService.updateSysNews(sysNews));
    }

    /**
     * 推荐首页轮播
     */
    @PreAuthorize("@ss.hasPermi('system:news:edit')")
    @Log(title = "新闻", businessType = BusinessType.UPDATE)
    @PutMapping("/changeHotFlag")
    public AjaxResult changeHotFlag(@RequestBody SysNews sysNews)
    {
        if ("1".equals(sysNews.getHotFlag())){
            List<SysNewsVo> hotNewsList = sysNewsService.findHotNewsList();
            //最多只能设置3个轮播
            if (hotNewsList.size() >= 3){
                return AjaxResult.error("最多只能设置3个首页轮播新闻");
            }
        }

        SysNewsVo sysNewsVo = sysNewsService.selectSysNewsByNewsId(sysNews.getNewsId());
        if (StringUtils.isEmpty(sysNewsVo.getImage())){
            return AjaxResult.error("该新闻没有主图，不能设置成");
        }

        return toAjax(sysNewsService.updateSysNews(sysNews));
    }

    /**
     * 删除新闻
     */
    @PreAuthorize("@ss.hasPermi('system:news:remove')")
    @Log(title = "新闻", businessType = BusinessType.DELETE)
	@DeleteMapping("/{newsIds}")
    public AjaxResult remove(@PathVariable Long[] newsIds)
    {
        return toAjax(sysNewsService.deleteSysNewsByNewsIds(newsIds));
    }

    @GetMapping(value = "/find")
    public TableDataInfo find(SysNews sysNews)
    {
        startPage();
        sysNews.setStatus("1"); //只查询生效的数据
        List<SysNewsVo> list = sysNewsService.selectSysNewsList(sysNews);
        return getDataTable(list);
    }

    @GetMapping(value = "/id/{newsId}")
    public AjaxResult findById(@PathVariable("newsId") Long newsId)
    {
        SysNewsVo sysNewsVo = sysNewsService.selectSysNewsByNewsId(newsId);
        //更新浏览量＋1
        sysNewsVo.setViews(sysNewsVo.getViews() + 1);
        sysNewsService.updateSysNews(sysNewsVo);
        //上一条
        SysNews previous = sysNewsService.getPrevious(sysNewsVo);
        sysNewsVo.setPrevious(previous);
        //下一条
        SysNews next = sysNewsService.getNext(sysNewsVo);
        sysNewsVo.setNext(next);
        return AjaxResult.success(sysNewsVo);
    }


    /**
     * 热点新闻
     */
    @GetMapping(value = "/findHotNewsList")
    public AjaxResult findHotNewsList()
    {
        List<SysNewsVo> list = sysNewsService.findHotNewsList();
        return AjaxResult.success(list);
    }

    /**
     * 国内动态
     */
    @GetMapping(value = "/findDomesticDynamicsNews")
    public TableDataInfo findDomesticDynamicsNews()
    {
        startPage();
        List<SysNewsVo> list = sysNewsService.findDomesticDynamicsNews();
        return getDataTable(list);
    }

    /**
     * 省内动态
     */
    @GetMapping(value = "/findProvincialDynamicsNews")
    public TableDataInfo findProvincialDynamicsNews()
    {
        Long areaId = null;
        SysArea areaParam = new SysArea();
        areaParam.setAreaName("广东省");
        List<SysArea> sysAreas = areaService.selectSysAreaList(areaParam);
        if (CollectionUtils.isEmpty(sysAreas)){
            areaId = sysAreas.get(0).getAreaId();
        }
        SysNews sysNews = new SysNews();
        sysNews.setAreaId(areaId);
        startPage();
        List<SysNewsVo> list = sysNewsService.findProvincialDynamicsNews(sysNews);
        return getDataTable(list);
    }

    /**
     * 地市分享 -- 推荐，仅返回3条
     */
    @GetMapping(value = "/findCitySharingRecommend")
    public AjaxResult findCitySharingRecommend()
    {
        List<SysNews> list = sysNewsService.findCitySharingRecommend();
        return AjaxResult.success(list);
    }

    /**
     * 员工分享 -- 推荐，仅返回1条
     */
    @GetMapping(value = "/findEmployeeSharingRecommend")
    public AjaxResult findEmployeeSharingRecommend()
    {
        List<SysNews> list = sysNewsService.findEmployeeSharingRecommend();
        return AjaxResult.success(list);
    }

    /**
     * 根据id 返回当前新闻 和 上一条，下一条
     */
    @GetMapping(value = "/findPreviousAndNext")
    public AjaxResult findPreviousAndNext(SysNews sysNews)
    {
        SysNewsVo sysNewsVo = sysNewsService.selectSysNewsByNewsId(sysNews.getNewsId());
        if (!ObjectUtils.isEmpty(sysNews)){
            SysNews previous = sysNewsService.getPrevious(sysNews);
            sysNewsVo.setPrevious(previous);
            SysNews next = sysNewsService.getNext(sysNews);
            sysNewsVo.setNext(next);
        }
        return AjaxResult.success(sysNewsVo);
    }
}
